package com.lindroy.androidplayer.viewmodel

import androidx.lifecycle.MutableLiveData
import com.lindroy.androidplayer.base.viewmodel.BaseViewModel
import com.lindroy.androidplayer.extensions.request
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.ProjectItemResp
import com.lindroy.androidplayer.logic.model.ProjectTypesResp
import com.lindroy.androidplayer.logic.network2.state.ResultState
import com.lindroy.androidplayer.logic.network2.apiService
import com.lindroy.androidplayer.logic.network2.statecallback.ListDataState

/**
 * @author Lin
 * @date 2021/4/9
 * @function
 */
class ProjectViewModel : BaseViewModel() {

    private var pageNo = 1

    var cid = -1

    val projectTypesLiveData: MutableLiveData<ResultState<List<ProjectTypesResp>>> =
        MutableLiveData()

    val projectListLiveData: MutableLiveData<ListDataState<ProjectItemResp>> =
        MutableLiveData()

    fun reqProjectList(isRefresh: Boolean) {
        isRefresh.yes { pageNo = 1 }
        request({ apiService.getProjectList(pageNo, cid) }, {
            projectListLiveData.value = ListDataState(
                success = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.datas
            )
            pageNo++
        }, {
            projectListLiveData.value = ListDataState(
                success = false,
                isRefresh = isRefresh,
                errMessage = it.errorMsg,
                listData = arrayListOf()
            )
        })
    }

    /**
     * 获取项目目录
     */
    fun reqProjectTypes() {
        request({ apiService.getProjectTypes() }, projectTypesLiveData, isShowDialog = true)
    }

}